/**
 * Copyright (c) 2011, Jason Gardner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.freecake.bonejams;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.freecake.bonejams.database.MusicCollection;
import org.freecake.bonejams.database.MusicCollection.MusicCollectionBinder;
import org.freecake.bonejams.model.Album;
import org.freecake.bonejams.model.Mp3File;

import java.util.ArrayList;
import java.util.List;

public class AlbumListView extends ListActivity {
    
    private ArrayList<Album> albums;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        albums = ((BoneJamsApplication) this.getApplication()).getMusicCollection().getAllAlbums();   
		this.setListAdapter(new AlbumListViewAdapter(this, R.layout.album_listview_item, albums));
		this.setTitle(this.getTitle() + " - " + albums.size() + " album(s)");
    }
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Album album = (Album) this.getListAdapter().getItem(position);
		loadAlbumForPlayback(album);
	}   
	
	private void loadAlbumForPlayback(Album album) {
	    ArrayList<Mp3File> songs = ((BoneJamsApplication) this.getApplication())
	                                .getMusicCollection().getSongsOnAlbum(album);
        Intent intent = new Intent(AlbumListView.this.getApplication(), PlaybackView.class);
        ((BoneJamsApplication) this.getApplication()).setCurrentSongList(songs);
        startActivity(intent);
	}
	
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(AlbumListView.this.getApplication(), MenuView.class);
        startActivity(intent);
        
    }	
        
    @Override
    protected void onUserLeaveHint() {
        //End the application if the user navigates away (home screen)
        super.onUserLeaveHint();
        finish();
    }       
       
    @Override
    protected void onStop() {
        //End the application if another application takes focus
        super.onStop();
        finish();
    }
        
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }  
    
    public class AlbumListViewAdapter extends ArrayAdapter<String> {
    
        public AlbumListViewAdapter(Context context, int textViewResourceId, List objects) {
            super(context, textViewResourceId, objects);
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.album_listview_item, parent, false);
            ((TextView)view.findViewById(R.id.albumlabel)).setText(albums.get(position).title);
            ((TextView)view.findViewById(R.id.albumartistlabel)).setText(albums.get(position).artist);
            return view;
        }
    };    
}
